//
//  SearchHistoryView.m
//  dhfsAPP
//
//  Created by Devil on 2017/8/7.
//  Copyright © 2017年 Devil. All rights reserved.
//

#import "SearchHistoryView.h"
@interface SearchHistoryView()

@property(strong,nonatomic)NSArray *historyArray,*hotContentArray;

@property(strong,nonatomic)UILabel *historyTitle;

@property(strong,nonatomic)UIButton *deleteBtn;

@end

@implementation SearchHistoryView

#pragma mark - 懒加载
-(UIView *)historyView{
    if (!_historyView) {
        _historyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        
        [_historyView addSubview:self.historyTitle];
        [_historyView addSubview:self.deleteBtn];
        
        if (self.historyArray.count > 0) {
            self.historyTitle.hidden = NO;
            self.deleteBtn.hidden = NO;
        }
        NSLog(@"%@",self.historyArray);
        
        UIButton *lastBtn;
        for (int i = 0; i < self.historyArray.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            
            btn.frame = CGRectMake(WIDTH(21), HEIGHT(102), 0, 0);
            
            [btn setTitle:self.historyArray[self.historyArray.count - 1 - i][2] forState:UIControlStateNormal];
            
            [btn setTitleColor:RGB(0, 105, 193) forState:UIControlStateNormal];
            
            btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];
            
            [btn sizeToFit];
            if (i == 0)
            {
                btn.frame = CGRectMake(WIDTH(21), HEIGHT(102), btn.frame.size.width + WIDTH(40), HEIGHT(50));
            }
            else
            {
                btn.frame = CGRectMake(lastBtn.frame.origin.x + lastBtn.frame.size.width + WIDTH(36), lastBtn.frame.origin.y, btn.frame.size.width + WIDTH(40), HEIGHT(50));
                
                if (btn.frame.size.width + btn.frame.origin.x > SCREEN_WIDTH)
                {
                    btn.frame = CGRectMake(WIDTH(21), lastBtn.frame.origin.y + HEIGHT(72), btn.frame.size.width, HEIGHT(50));
                }
            }
            
            lastBtn = btn;
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            
            btn.tag = 1000 + i;
            
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            
            btn.layer.masksToBounds = YES;
            btn.layer.borderColor = RGB(0, 105, 193).CGColor;
            btn.layer.borderWidth = WIDTH(1);
            btn.layer.cornerRadius = 10.0;
            
            
            [_historyView addSubview:btn];
        }
        if (lastBtn != nil)
        {
            _historyView.frame = CGRectMake(0, 0, SCREEN_WIDTH, lastBtn.frame.origin.y + HEIGHT(72));

            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, lastBtn.frame.origin.y + HEIGHT(72), SCREEN_WIDTH, HEIGHT(3))];
            
            lineView.backgroundColor = RGB(221, 221, 221);
            
            [_historyView addSubview:lineView];
        }
    }

    return _historyView;
}

-(UILabel *)historyTitle{
    if (!_historyTitle) {
        _historyTitle = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(22), HEIGHT(26), 0, 0)];
        
        _historyTitle.text = @"搜索历史";
        _historyTitle.textColor = RGB(51, 51, 51);
        _historyTitle.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
        
        [_historyTitle sizeToFit];
        
        _historyTitle.hidden = YES;
    }
    
    return _historyTitle;
}

-(UIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_deleteBtn setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
        
        _deleteBtn.frame = CGRectMake(WIDTH(695), HEIGHT(26), WIDTH(35), HEIGHT(39));
        
        _deleteBtn.tag = 101;
        
        [_deleteBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        _deleteBtn.hidden = YES;
    }
    
    return _deleteBtn;
}

-(UIView *)hotContentView{
    if (!_hotContentView) {
        _hotContentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.historyView.frame.size.height + self.historyView.frame.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT(128) - self.historyView.frame.size.height)];
        
        UILabel *hotContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH(22), HEIGHT(26), 0, 0)];
        
        hotContentLabel.text = @"热门搜索";
        hotContentLabel.textColor = RGB(51, 51, 51);
        hotContentLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(32)];
        
        [hotContentLabel sizeToFit];
        
        [_hotContentView addSubview:hotContentLabel];

    }
    
    return _hotContentView;
}
#pragma mark - 构造函数
-(instancetype)initWithFrame:(CGRect)frame HistoryArray:(NSArray *)historyArray{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.historyArray = historyArray;
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self initUI];
        
    }
    
    return self;
}

#pragma mark - 其它函数
-(void)initUI{
    [self addSubview:self.historyView];
    [self addSubview:self.hotContentView];
    
}

-(void)changeHotContentView:(NSArray *)hotContentArray{
    self.hotContentArray = hotContentArray;
//    NSLog(@"%@",hotContentArray);
    UIButton *lastBtn;
    for (int i = 0; i < self.hotContentArray.count; i ++)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        btn.frame = CGRectMake(WIDTH(21), HEIGHT(102), 0, 0);
        
        [btn setTitle:self.hotContentArray[self.hotContentArray.count - 1 - i][@"name"] forState:UIControlStateNormal];
        
        [btn setTitleColor:RGB(0, 105, 193) forState:UIControlStateNormal];
        
        btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];
        
        [btn sizeToFit];
        if (i == 0)
        {
            btn.frame = CGRectMake(WIDTH(21), HEIGHT(102), btn.frame.size.width + WIDTH(40), HEIGHT(50));
        }
        else
        {
            btn.frame = CGRectMake(lastBtn.frame.origin.x + lastBtn.frame.size.width + WIDTH(36), lastBtn.frame.origin.y, btn.frame.size.width + WIDTH(40), HEIGHT(50));
            
            if (btn.frame.size.width + btn.frame.origin.x > SCREEN_WIDTH)
            {
                btn.frame = CGRectMake(WIDTH(21), lastBtn.frame.origin.y + HEIGHT(72), btn.frame.size.width, HEIGHT(50));
            }
        }
        
        lastBtn = btn;
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        btn.tag = 2000 + i;
        
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.layer.masksToBounds = YES;
        btn.layer.borderColor = RGB(0, 105, 193).CGColor;
        btn.layer.borderWidth = WIDTH(1);
        btn.layer.cornerRadius = 10.0;
        
        
        [_hotContentView addSubview:btn];
    }

}

-(void)changeHistory:(NSArray *)historyArray{
    for (id obj in _historyView.subviews) {
        if ([obj isKindOfClass:[UIButton class]]) {
            [obj removeFromSuperview];
        }
    }
    self.historyArray = historyArray;
    _historyView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);
    
    if (self.historyArray.count > 0)
    {
        self.historyTitle.hidden = NO;
        self.deleteBtn.hidden = NO;
    }
    else
    {
        self.historyTitle.hidden = YES;
        self.deleteBtn.hidden = YES;
    }
    NSLog(@"%@",self.historyArray);
    
    UIButton *lastBtn;
    for (int i = 0; i < self.historyArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        btn.frame = CGRectMake(WIDTH(21), HEIGHT(102), 0, 0);
        
        [btn setTitle:self.historyArray[self.historyArray.count - 1 - i][2] forState:UIControlStateNormal];
        
        [btn setTitleColor:RGB(0, 105, 193) forState:UIControlStateNormal];
        
        btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:HEIGHT(28)];
        
        [btn sizeToFit];
        if (i == 0)
        {
            btn.frame = CGRectMake(WIDTH(21), HEIGHT(102), btn.frame.size.width + WIDTH(40), HEIGHT(50));
        }
        else
        {
            btn.frame = CGRectMake(lastBtn.frame.origin.x + lastBtn.frame.size.width + WIDTH(36), lastBtn.frame.origin.y, btn.frame.size.width + WIDTH(40), HEIGHT(50));
            
            if (btn.frame.size.width + btn.frame.origin.x > SCREEN_WIDTH)
            {
                btn.frame = CGRectMake(WIDTH(21), lastBtn.frame.origin.y + HEIGHT(72), btn.frame.size.width, HEIGHT(50));
            }
        }
        
        lastBtn = btn;
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        btn.tag = 1000 + i;
        
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.layer.masksToBounds = YES;
        btn.layer.borderColor = RGB(0, 105, 193).CGColor;
        btn.layer.borderWidth = WIDTH(1);
        btn.layer.cornerRadius = 10.0;
        
        
        [_historyView addSubview:btn];
    }
    if (lastBtn != nil)
    {
        _historyView.frame = CGRectMake(0, 0, SCREEN_WIDTH, lastBtn.frame.origin.y + HEIGHT(72));
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, lastBtn.frame.origin.y + HEIGHT(72), SCREEN_WIDTH, HEIGHT(3))];
        
        lineView.backgroundColor = RGB(221, 221, 221);
        
        [_historyView addSubview:lineView];
    }
    
    self.hotContentView.frame = CGRectMake(0, self.historyView.frame.origin.y + self.historyView.frame.size.height, SCREEN_WIDTH, SCREEN_HEIGHT - HEIGHT(128) - self.historyView.frame.size.width);

}

-(void)click:(UIButton *)btn{
    if (btn.tag == 101)
    {
        if ([self.searchHistoryDelegate respondsToSelector:@selector(deleteHistory)])
        {
            [self.searchHistoryDelegate deleteHistory];
        }
    }
    else if (btn.tag > 999 && btn.tag < 2000)
    {
        if ([self.searchHistoryDelegate respondsToSelector:@selector(clickHistory:)]) {
            [self.searchHistoryDelegate clickHistory:(int)btn.tag - 1000];
        }
    }
    else if (btn.tag >= 2000)
    {
        if ([self.searchHistoryDelegate respondsToSelector:@selector(clickHotContent:)])
        {
            [self.searchHistoryDelegate clickHotContent:(int)btn.tag - 2000];
        }
    }
}

@end
